import 'package:simple_server/src/context.dart';
import 'package:simple_server/src/simple_server_base.dart';
export 'package:simple_server/src/middleware/auth_middleware.dart';
export 'package:simple_server/src/middleware/body_parser_middleware.dart';
export 'package:simple_server/src/middleware/cross_origin_middleware.dart';
export 'package:simple_server/src/middleware/logging_middleware.dart';
export 'package:simple_server/src/middleware/mapping_middleware.dart';

typedef Next = Future<void> Function();

abstract class Middleware<T extends Context> {
  SimpleServer<T>? _server;

  SimpleServer<T> get server {
    if (_server == null) {
      throw StateError('Middleware has not been added to a SimpleServer');
    }
    return _server!;
  }

  set server(SimpleServer<T> server) {
    if (_server != null) {
      throw StateError('Middleware is already in use by another SimpleServer');
    }
    _server = server;
  }

  Future<void> call(T context, Next next);
}
